'''
https://leetcode.cn/problems/implement-stack-using-queues/description/
'''


class MyStack:

    def __init__(self):
        self.in_queue = []
        self.out_queue = []
        self.size = 0

    def push(self, x: int) -> None:
        self.size += 1
        self.in_queue.append(x)

    def pop(self) -> int:
        self.size -= 1
        while len(self.in_queue) > 1:
            self.out_queue.append(self.in_queue.pop(0))
        res = self.in_queue.pop(0)
        self.in_queue, self.out_queue = self.out_queue, self.in_queue
        return res

    def top(self) -> int:
        while len(self.in_queue) > 1:
            self.out_queue.append(self.in_queue.pop(0))
        res = self.in_queue[0]
        self.out_queue.append(self.in_queue.pop(0))
        self.in_queue, self.out_queue = self.out_queue, self.in_queue
        return res

    def empty(self) -> bool:
        return self.size == 0
# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()